// axios-wrapper.ts
import axios, { AxiosRequestConfig, AxiosInstance } from 'axios';

//请求响应数据类型
interface Data<T>{
    code:string,
    msg:string,
    result:T
}

class AxiosWrapper {
  private instance: AxiosInstance;

  constructor() {
    this.instance = axios.create({});
  }

  public async request<T>(config: AxiosRequestConfig): Promise<Data<T>> {
    try {
      const response = await this.instance(config);
      return {
        result: response.data.result,
        code: response.data.code,
        msg: response.data.msg,
      };
    } catch (error) {
      throw error;
    }
  }

  public setBaseURL(baseURL: string): void {
    this.instance.defaults.baseURL = baseURL;
  }

  public setHeaders(headers: any): void {
    this.instance.defaults.headers.common = headers;
  }

  public setTimeout(timeout: number): void {
    this.instance.defaults.timeout = timeout;
  }
}

export default AxiosWrapper;


// const api = new AxiosWrapper();
// api.setBaseURL('/api');
// api.setHeaders({ 'source-client': 'miniapp' });
// api.setTimeout(5000);

// export default api;